#include "Bar3D.h"

//#include "qbitmap.h"

#include "qwt3d_color.h"
#include "qwt3d_plot.h"

//-------------------------------------

Bar3D::Bar3D()
{
    useMesh = false;
    configure( 0.0005, 0.5 );
}

//-------------------------------------

Bar3D::Bar3D( double rad, double level )
{
    useMesh = true;
    configure( rad, level );
}

//-------------------------------------

void Bar3D::configure( double rad, double level )
{
    plot = 0;
    radius_ = rad;
    level_ = level;
}

//-------------------------------------

void Bar3D::drawBegin()
{
    diag_ = ( plot->hull().maxVertex - plot->hull().minVertex ).length() * radius_;
    glLineWidth( 0 );
    glEnable( GL_POLYGON_OFFSET_FILL );
    glPolygonOffset( 1, 1 );
}

//-------------------------------------

void Bar3D::drawEnd()
{
}

//-------------------------------------

void Bar3D::draw( Qwt3D::Triple const & pos )
{
    //	GLStateBewarer sb(GL_LINE_SMOOTH, true);
    //  sb.turnOn();

    double interval = plot->hull().maxVertex.z - plot->hull().minVertex.z;
    double numlevel = plot->hull().minVertex.z + level_ * interval;
    interval /= 100;
    if( pos.z > numlevel - interval && pos.z < numlevel + interval )
    {
        //Label3D lb;
        //lb.draw( pos, diag_, diag_ * 2 );
    }

    GLdouble minz = plot->hull().minVertex.z;

    Qwt3D::RGBA rgbat = (*plot->dataColor())( pos );
    Qwt3D::RGBA rgbab = (*plot->dataColor())( pos.x, pos.y, minz );

    glBegin( GL_QUADS );
        glColor4d( rgbab.r, rgbab.g, rgbab.b, rgbab.a );
        glVertex3d( pos.x-diag_, pos.y-diag_, minz );
        glVertex3d( pos.x+diag_, pos.y-diag_, minz );
        glVertex3d( pos.x+diag_, pos.y+diag_, minz );
        glVertex3d( pos.x-diag_, pos.y+diag_, minz );

        if( pos.z > numlevel - interval && pos.z < numlevel + interval )
            glColor3d( 0.7, 0, 0 );
        else
            glColor4d( rgbat.r, rgbat.g, rgbat.b, rgbat.a );

        glVertex3d( pos.x-diag_, pos.y-diag_, pos.z );
        glVertex3d( pos.x+diag_ ,pos.y-diag_, pos.z );
        glVertex3d( pos.x+diag_, pos.y+diag_, pos.z );
        glVertex3d( pos.x-diag_, pos.y+diag_, pos.z );

        glColor4d( rgbab.r, rgbab.g, rgbat.b, rgbab.a );
        glVertex3d( pos.x-diag_, pos.y-diag_, minz );
        glVertex3d( pos.x+diag_, pos.y-diag_, minz );
        glColor4d( rgbat.r,rgbat.g, rgbat.b, rgbat.a );
        glVertex3d( pos.x+diag_, pos.y-diag_, pos.z );
        glVertex3d( pos.x-diag_, pos.y-diag_, pos.z );

        glColor4d( rgbab.r, rgbab.g, rgbat.b, rgbab.a );
        glVertex3d( pos.x-diag_, pos.y+diag_, minz );
        glVertex3d( pos.x+diag_, pos.y+diag_, minz );
        glColor4d( rgbat.r, rgbat.g, rgbat.b, rgbat.a );
        glVertex3d( pos.x+diag_, pos.y+diag_, pos.z );
        glVertex3d( pos.x-diag_, pos.y+diag_, pos.z );

        glColor4d( rgbab.r, rgbab.g, rgbat.b, rgbab.a );
        glVertex3d( pos.x-diag_, pos.y-diag_, minz );
        glVertex3d( pos.x-diag_, pos.y+diag_, minz );
        glColor4d( rgbat.r, rgbat.g, rgbat.b, rgbat.a );
        glVertex3d( pos.x-diag_, pos.y+diag_, pos.z );
        glVertex3d( pos.x-diag_, pos.y-diag_, pos.z );

        glColor4d( rgbab.r, rgbab.g, rgbat.b, rgbab.a );
        glVertex3d( pos.x+diag_, pos.y-diag_, minz );
        glVertex3d( pos.x+diag_, pos.y+diag_, minz );
        glColor4d( rgbat.r, rgbat.g, rgbat.b, rgbat.a );
        glVertex3d( pos.x+diag_, pos.y+diag_, pos.z );
        glVertex3d( pos.x+diag_, pos.y-diag_, pos.z );
    glEnd();

    if( useMesh )
        glColor3d( 0, 0, 0 );
    else
        glColor4d( 1, 1, 1, 0.3 );

    glBegin( GL_LINES );
        glVertex3d( pos.x-diag_, pos.y-diag_, minz );
        glVertex3d( pos.x+diag_, pos.y-diag_, minz );
        glVertex3d( pos.x-diag_, pos.y-diag_, pos.z );
        glVertex3d( pos.x+diag_, pos.y-diag_, pos.z );
        glVertex3d( pos.x-diag_, pos.y+diag_, pos.z );
        glVertex3d( pos.x+diag_, pos.y+diag_, pos.z );
        glVertex3d( pos.x-diag_, pos.y+diag_, minz );
        glVertex3d( pos.x+diag_, pos.y+diag_, minz );

        glVertex3d( pos.x-diag_, pos.y-diag_, minz );
        glVertex3d( pos.x-diag_, pos.y+diag_, minz );
        glVertex3d( pos.x+diag_, pos.y-diag_, minz );
        glVertex3d( pos.x+diag_, pos.y+diag_, minz );
        glVertex3d( pos.x+diag_, pos.y-diag_, pos.z );
        glVertex3d( pos.x+diag_, pos.y+diag_ ,pos.z );
        glVertex3d( pos.x-diag_, pos.y-diag_, pos.z );
        glVertex3d( pos.x-diag_, pos.y+diag_, pos.z );

        glVertex3d( pos.x-diag_, pos.y-diag_, minz );
        glVertex3d( pos.x-diag_, pos.y-diag_, pos.z );
        glVertex3d( pos.x+diag_, pos.y-diag_, minz );
        glVertex3d( pos.x+diag_, pos.y-diag_, pos.z );
        glVertex3d( pos.x+diag_, pos.y+diag_, minz );
        glVertex3d( pos.x+diag_, pos.y+diag_, pos.z);
        glVertex3d( pos.x-diag_, pos.y+diag_, minz );
        glVertex3d( pos.x-diag_, pos.y+diag_, pos.z );
    glEnd();
}

//-------------------------------------

